import java.util.ArrayList;
import java.util.Random;
public class Exercise {
    // 存放算式的动态数组
    private ArrayList<BinaryOperation> operationList = new ArrayList<BinaryOperation>();
    private int current = 0; // 动态数组的游标
    /**
     * 产生加法算式习题
     * @param operationCount：习题中算式的个数
     */
    public void generateAdditionExercise(int operationCount) {
        BinaryOperation anOperation;
        while(operationCount > 0) {
            do {
                anOperation = new AdditionOperation();
            }while(operationList.contains(anOperation));
            operationList.add(anOperation);
            operationCount--;
        }
    }

    /**
     * 产生减法算式习题
     * @param operationCount：习题中算式的个数
     */
    public void generateSubstractExercise(int operationCount) {
        BinaryOperation anOperation;
        while(operationCount > 0) {
            do {
                anOperation = new SubstractOperation();
            }while(operationList.contains(anOperation));
            operationList.add(anOperation);
            operationCount--;
        }
    }

    /**
     * 产生加减法混合算式习题
     * @param operationCount：习题中算式的个数
     */
    public void generateBinaryExercise(int operationCount) {
        BinaryOperation anOperation;
        Random random = new Random();
        while(operationCount > 0) {
            do {
                int opValue = random.nextInt(2);
                if(opValue == 0)
                    anOperation = new AdditionOperation();
                else
                    anOperation = new SubstractOperation();
            }while(operationList.contains(anOperation));
            operationList.add(anOperation);
            operationCount--;
        }
    }

    /**
     * 遍历判断是否有下一个算式
     * @return
     */
    public boolean hasNext() {
        return current <= operationList.size()-1;
    }

    /**
     * 遍历返回当前算式，游标+1
     * @return 当前算式
     */
    public BinaryOperation next() {
        return operationList.get(current++);
    }

}

